﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using YachtCaptain.Game_States;


namespace YachtCaptain.Game_States
{
    class CGameplayState : IBaseState
    {
        //FOR SINGLETON
        private static CGameplayState instance;
        Game Game1OBJ;

        //PLAYER
        Player m_Player1 = new Player();

        //TEXTURES
        List<Texture2D> m_LGameplayTextures;

        private CGameplayState() { }

        public static CGameplayState GetInstance()
        {
            if (instance == null)
            {
                instance = new CGameplayState();
            }
            return instance;
        }

        public override void LoadContent()
        {
            m_LGameplayTextures = Game1OBJ.LoadGameplayContent();
        }

        public override void Initialize(Game obj)
        {
            Game1OBJ = obj;
            LoadContent();
        }

        public override void Update()
        {
            if (m_Player1.GetPath().Count >= 1)
            {
                Vector2 newPos = new Vector2();
                if (m_Player1.GetPath()[0].X < m_Player1.GetCurrentPosition().X)
                    newPos.X = -m_Player1.GetBoatSpeed();
                if (m_Player1.GetPath()[0].X > m_Player1.GetCurrentPosition().X)
                    newPos.X = m_Player1.GetBoatSpeed();
                if (m_Player1.GetPath()[0].Y < m_Player1.GetCurrentPosition().Y)
                    newPos.Y = -m_Player1.GetBoatSpeed();
                if (m_Player1.GetPath()[0].Y > m_Player1.GetCurrentPosition().Y)
                    newPos.Y = m_Player1.GetBoatSpeed();

                if (m_Player1.GetCurrentPosition().X >= (m_Player1.GetPath()[0].X -5) &&
                    m_Player1.GetCurrentPosition().X <= (m_Player1.GetPath()[0].X + 5) &&
                    m_Player1.GetCurrentPosition().Y >= (m_Player1.GetPath()[0].Y -5) &&
                    m_Player1.GetCurrentPosition().Y <= (m_Player1.GetPath()[0].Y + 5))
                {
                    m_Player1.GetPath().RemoveAt(0);
                }

                m_Player1.SetPosition(newPos);
            }
        }

        public override void Render(SpriteBatch spriteB)
        {
            Vector2 pos = new Vector2(0.0f, 0.0f);
            spriteB.Draw(m_LGameplayTextures[0], pos, Color.White);

            spriteB.Draw(m_LGameplayTextures[1], m_Player1.GetCurrentPosition(), Color.White);
        }

        public override void Input()
        {
            if (Game1OBJ.GetMouseState().LeftButton == ButtonState.Pressed &&
                Game1OBJ.GetMouseState().X >= 0)// && Game1OBJ.GetMouseState().X)
            {
                Vector2 MousePos;
                MousePos.X = Game1OBJ.GetMouseState().X;
                MousePos.Y = Game1OBJ.GetMouseState().Y;

                m_Player1.GetPath().Add(MousePos);
            }
        }

        public override void Shutdown()
        {
            
        }
    }
}
